#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>

struct NTHREADPOOL;

//线程队列(工作队列)
struct NWORKER
{
    pthread_t id;             //线程ID
    struct NTHREADPOOL* pool; //线程池

    struct NWORKER* prev;
    struct NWORKER* next;
};

//任务队列
struct NTASK
{
    void (*task_func)(void *arg);
    void *user_date;

    struct NTASK* prev;
    struct NTASK* next;
};

//线程池(管理组件)
typedef struct NTHREADPOOL
{
    struct NWORKER* workers;        //工作队列(头指针)
    struct NTASK*   tasks;          //任务队列(头指针)

    struct NTASK*   tasks_end;      //任务队列(尾指针)

    int terminate;                  //是否销毁线程
    __uint32_t task_size;           //任务数量

    pthread_mutex_t mtx;            //互斥量
    pthread_cond_t cond;            //条件变量
}nthreadpool_t;


//线程入口函数
static void* threadd_callback(void* arg);

//创建线程池
int thread_pool_create(nthreadpool_t* pool, int nthread);

//向链表头部添加任务
int thread_pool_push_front(nthreadpool_t* pool, struct NTASK* task);

//向链表尾部添加任务
int thread_pool_push_back(nthreadpool_t* pool, struct NTASK* task);

//销毁线程池
int thread_pool_destroy(nthreadpool_t* pool);

//返回当前的任务数量
__uint32_t get_task_size(nthreadpool_t* pools);